﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DeveloperUtility.Security;

using NetExp.MediaXenter.Database;

namespace NetExp.MediaXenter.Utilities
{
    public static class KeyboardHelper
    {
        public static KeyboardDS ReadKeyboardFile()
        {

            KeyboardDS keyboardDS = new KeyboardDS();

            FileInfo fInfo = new FileInfo(Constants.SETTINGSPATH + Constants.FILEKEYBOARDNAME);
            if (!fInfo.Exists)
            {
                DirectoryInfo dInfo = new DirectoryInfo(Constants.SETTINGSPATH);
                dInfo.Create();
                DefaultKeyboardSetting(keyboardDS);
            }
            keyboardDS.ReadXml(Constants.SETTINGSPATH + Constants.FILEKEYBOARDNAME);

            return keyboardDS;
        }

        public static void DefaultKeyboardSetting(KeyboardDS keyboardDS)
        {
            KeyboardDS.FunctionsRow function1 = keyboardDS.Functions.NewFunctionsRow();
            KeyboardDS.FunctionsRow function2 = keyboardDS.Functions.NewFunctionsRow();
            KeyboardDS.FunctionsRow function3 = keyboardDS.Functions.NewFunctionsRow();
            KeyboardDS.FunctionsRow function4 = keyboardDS.Functions.NewFunctionsRow();
            KeyboardDS.FunctionsRow function5 = keyboardDS.Functions.NewFunctionsRow();
            KeyboardDS.FunctionsRow function6 = keyboardDS.Functions.NewFunctionsRow();
            KeyboardDS.FunctionsRow function7 = keyboardDS.Functions.NewFunctionsRow();
            function1.Id = 1;
            function1.Name = "Exit";
            function2.Id = 2;
            function2.Name = "MoveUp";
            function3.Id = 3;
            function3.Name = "MoveDown";
            function4.Id = 4;
            function4.Name = "MoveLeft";
            function5.Id = 5;
            function5.Name = "MoveRight";
            function6.Id = 6;
            function6.Name = "Selected";
            function7.Id = 7;
            function7.Name = "Back";

            keyboardDS.Functions.AddFunctionsRow(function1);
            keyboardDS.Functions.AddFunctionsRow(function2);
            keyboardDS.Functions.AddFunctionsRow(function3);
            keyboardDS.Functions.AddFunctionsRow(function4);
            keyboardDS.Functions.AddFunctionsRow(function5);
            keyboardDS.Functions.AddFunctionsRow(function6);
            keyboardDS.Functions.AddFunctionsRow(function7);

            KeyboardDS.KeysRow chiave1 = keyboardDS.Keys.NewKeysRow();
            KeyboardDS.KeysRow chiave2 = keyboardDS.Keys.NewKeysRow();
            KeyboardDS.KeysRow chiave3 = keyboardDS.Keys.NewKeysRow();
            KeyboardDS.KeysRow chiave4 = keyboardDS.Keys.NewKeysRow();
            KeyboardDS.KeysRow chiave5 = keyboardDS.Keys.NewKeysRow();
            KeyboardDS.KeysRow chiave6 = keyboardDS.Keys.NewKeysRow();
            KeyboardDS.KeysRow chiave7 = keyboardDS.Keys.NewKeysRow();

            chiave1.IdFunction = 1;
            chiave1.Key = "Esc";

            chiave2.IdFunction = 2;
            chiave2.Key = "Up";

            chiave3.IdFunction = 3;
            chiave3.Key = "Down";

            chiave4.IdFunction = 4;
            chiave4.Key = "Left";

            chiave5.IdFunction = 5;
            chiave5.Key = "Right";

            chiave6.IdFunction = 6;
            chiave6.Key = "Enter";

            chiave7.IdFunction = 7;
            chiave7.Key = "Back";

            keyboardDS.Keys.AddKeysRow(chiave1);
            keyboardDS.Keys.AddKeysRow(chiave2);
            keyboardDS.Keys.AddKeysRow(chiave3);
            keyboardDS.Keys.AddKeysRow(chiave4);
            keyboardDS.Keys.AddKeysRow(chiave5);
            keyboardDS.Keys.AddKeysRow(chiave6);
            keyboardDS.Keys.AddKeysRow(chiave7);

            keyboardDS.WriteXml(Constants.SETTINGSPATH + Constants.FILEKEYBOARDNAME);

            keyboardDS.Clear();
        }

        internal static void WriteSettings(SettingsDS settingsDS)
        {
            settingsDS.WriteXml(Constants.SETTINGSPATH + Constants.FILESETTINGSNAME);
        }
    }
}
